home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / dev / lang / HeliOS3.lha / helios_demo_disk3 / source / Demo7_SimpleMultiAnim.src < prev    next >
Encoding:
Text File  |  1995-11-11  |  29.2 KB  |  1,035 lines

  1.  
  2.   \ ***********************************************************
  3.   \
  4.   \                    MULTI ANIMATION DEMO
  5.   \
  6.   \ ***********************************************************
  7.   \
  8.   \ This code demonstrates how to create multiple animations and
  9.   \ then do collision detection.
  10.   \
  11.   \ This code builds upon the following Demos:
  12.   \
  13.   \ Demo1_SinglePF.src
  14.   \ Demo2_SinglePFCopper.src
  15.   \ Demo3_SimpleSprite.src
  16.   \ Demo4_MultiSprites.src
  17.   \ Demo5_MultiSptCollide.src
  18.   \ Demo6_SimpleAnim.src
  19.   \
  20.   \ ***********************************************************
  21.   \
  22.   \
  23.   \ Note that it is easy to change the number of animated objects.
  24.   \
  25.   \ All you have to do is change the value of the CONSTANT Alien#.
  26.   \
  27.   \ If you locate Alien#, you will see that it normally has a value
  28.   \ of 15.  Try changing this to another value.
  29.   \
  30.   \
  31.   \ ***********************************************************
  32.   \ Re-initialise HeliOS dictionary to standard CORE vocabulary
  33.   \ ***********************************************************
  34.  
  35.   \ This should always be used at the start of any program which
  36.   \ is to be repeatedly recompiled.
  37.  
  38.   FORGET **CORE**
  39.  
  40.   \ *************************
  41.   \ Load include symbol files
  42.   \ *************************
  43.   \
  44.   \ These "include files" are pre-compiled (for speed) versions of the
  45.   \ Amiga includes and the Helios system includes.
  46.   \
  47.   \ Uncomment the lines below for standalone compilation, but otherwise
  48.   \ it is better to set these include files from the Helios Forth menus
  49.  
  50.   AMIGAINCLUDE HeliOS:HeliOS_AmigaInclude
  51.   USERINCLUDE  HeliOS:HeliOS_UserInclude
  52.  
  53.   \ ****************************************
  54.   \ Create display imagery file name strings
  55.   \ ****************************************
  56.  
  57.   \ These files are ordinary IFF's (and may be "PowerPacked" if required)
  58.   \
  59.   \ The pictures here will be loaded into each of the display BitMaps.
  60.   \
  61.  
  62.   $CONSTANTL Slice1Pic $Helios:Source/Data/Pic1$
  63.  
  64.   \ **************************************
  65.   \ Create display configuration constants
  66.   \ **************************************
  67.   \
  68.   \ Collect all "display-specific" parameters here and generate "named"
  69.   \ constants which make references easier than using numeric values.
  70.   \
  71.   \ Collecting these together here makes it easier to adjust things at any
  72.   \ time without having to search source code to replace values individually.
  73.   \
  74.  
  75.  
  76.   256                      CONSTANT DisplayHeight      \ Full PAL display
  77.   320                      CONSTANT DisplayWidth       \ Lores display
  78.   44                       CONSTANT DisplayTopLine     \ Display start
  79.  
  80.   DisplayWidth             CONSTANT Slice1Width        \ Lores width
  81.   DisplayWidth 32 +        CONSTANT Slice1RasterWidth  \ Raster=SWidth+32
  82.   DisplayHeight            CONSTANT Slice1Height       \ Slice height
  83.   DisplayHeight 32 +       CONSTANT Slice1RasterHeight \ Slice Raster=DHgt+32
  84.   0                        CONSTANT Slice1Mode         \ Lores
  85.   3                        CONSTANT Slice1Planes       \ Slice bitplanes
  86.  
  87.   \ The calculation below takes the number of bitplanes and calculates
  88.   \ how many colours this represents.
  89.   \
  90.   \ One bitplane gives two colours.
  91.   \
  92.   \ Each additional bitplane multiplies the number of colours by two.
  93.   \
  94.   \ Performing a single LSL operation on any number multipies by 2, so we
  95.   \ have a quick method of multiplying by two for our colour calculation.
  96.   \
  97.   \ In this case, we need "2 to the power 3", which is 2*2*2=8.
  98.   \
  99.   \ e.g.                              2*2*2
  100.   \                                   ^ ^ ^
  101.   \                                   Total number of planes = 3
  102.   \
  103.   \
  104.   \ So, we take the first value two
  105.   \
  106.   \ e.g.                              2*2*2
  107.   \                                   ^
  108.   \                                  Initial start value of 2
  109.   \
  110.   \ and we now need to multiply it by two "the_number_of_planes minus_one"
  111.   \ more times.
  112.   \
  113.   \ e.g.                              2*2*2
  114.   \                                     ^ ^
  115.   \                                     Total planes minus one
  116.   \
  117.   \
  118.   \ So, Number of colours = 2 operated on by LSL NumberOfPlanes-1 times.
  119.   \
  120.  
  121.   2 Slice1Planes 1- LSL    CONSTANT Slice1Colours      \ A-slice colours
  122.  
  123.   \ ***********************
  124.   \ Error handling routines
  125.   \ ***********************
  126.  
  127.   \ This error handler allows all errors to be routed via a comprehensive
  128.   \ sequential closedown routine, which is associated with the HeliOS
  129.   \ system error handler word ERROR".
  130.   \
  131.   \ When ERROR" senses an error, it prints an associated error message
  132.   \ delimited by '"' characters, and then closes everything down using the
  133.   \ routine CLOSEDOWN below which you have supplied.
  134.   \
  135.   \ This simplifies errors checks to the use of a single word, ERROR", which
  136.   \ displays a text message and closes eveything down.
  137.   \
  138.  
  139.   0 VARIABLE (CLOSEDOWN)
  140.  
  141.   : ?CLOSEDOWNERROR
  142.  
  143.   IF
  144.     CR
  145.     CR
  146.     TYPE
  147.     CR
  148.     CR
  149.     ." Press <Space> to quit!"
  150.     CR
  151.     CR
  152.     WAITSPACE
  153.     (CLOSEDOWN) @EXECUTE
  154.     QUIT
  155.   ELSE
  156.     DDROP
  157.   THEN
  158.   ;
  159.  
  160.   LATESTCFA VARIABLE ERROR1
  161.  
  162.   \ ****************************************
  163.   \ Create display pointer storage variables
  164.   \ ****************************************
  165.  
  166.   \ Here we create a set of "pointers", initially set to a "null" value.
  167.   \
  168.   \ These "pointers" are set up as "long addresses" when various components
  169.   \ of the display system are allocated and initialised.
  170.   \
  171.   \ Note that initially these are all set to zero, and we clear them back
  172.   \ to zero when we de-allocate the associated resource.
  173.   \
  174.   \ These DPOINTERs are all initially set to "null" by using '0.'.
  175.   \
  176.   \ When we allocate memory or Amiga system resources in the program at
  177.   \ run-time, these pointers are updated to contain the 32-bit address
  178.   \ of the newly allocated resource.
  179.   \
  180.   \ Subsequently the symbolic DPOINTER name can be used in your code to
  181.   \ represent the associated address.
  182.  
  183.   0. DPOINTER Display1             \ Main Display structure pointer
  184.   0. DPOINTER Slice1               \ Slice 1 Slice structure pointer
  185.  
  186.   0. DPOINTER Slice1_ColorMap      \ Slice 1 ColourMap structure pointer
  187.  
  188.   0. DPOINTER Slice1_RasInfo       \ Slice 1 RasInfo structure pointer
  189.  
  190.   0. DPOINTER Slice1_BMap          \ Slice 1 BitMap structure pointer
  191.  
  192.   0. DPOINTER Slice1_SliceControl  \ Slice 1 SliceControl structure pointer
  193.  
  194.   \ *************
  195.   \ Colour tables
  196.   \ *************
  197.  
  198.   \ Each colour entry requies 2 bytes of storage space
  199.  
  200.   CREATEL Slice1_ColorTable        \ Create longword pointer to table
  201.   Slice1Colours 2* 0 ALLOTFILL     \ Allocate Slice1 colours * 2 bytes
  202.  
  203.  
  204.   \ *************************************
  205.   \ Copper strip for graduated background
  206.   \ *************************************
  207.  
  208.   0. DVARIABLE Copper              \ 32-bit CopperList pointer store
  209.   0. DVARIABLE CopperLength        \ 32-bit CopperList length store
  210.  
  211.   : AddCopper                      \ Add custom copper list to display
  212.  
  213.   Display1                         \ We are adding CopperList to Display1
  214.   Copper D@
  215.   ADDCOPPERSTRIP
  216.   SORTSTRIPTABLE
  217.   LINKSTRIPS
  218.   DDROP
  219.   ;
  220.  
  221.   : RemCopper                      \ Remove custom copper list from display
  222.  
  223.   Display1                         \ We are removing CopperList from Display1
  224.   Copper D@
  225.   DFLAG
  226.   IF
  227.     REMCOPPERSTRIP
  228.     LINKSTRIPS
  229.     DDROP
  230.   ELSE
  231.     DDDROP
  232.   THEN
  233.   ;
  234.  
  235.   : Create_Copper
  236.  
  237.   COPPERSTART
  238.   Copper D!
  239.   0 [ DECIMAL ]  45 [ HEX ] FFFE COPPERWAIT  DROP   0100 18E COPPERMOVE  DROP
  240.   0 [ DECIMAL ]  60 [ HEX ] FFFE COPPERWAIT  DROP   0200 18E COPPERMOVE  DROP
  241.   0 [ DECIMAL ]  75 [ HEX ] FFFE COPPERWAIT  DROP   0300 18E COPPERMOVE  DROP
  242.   0 [ DECIMAL ]  90 [ HEX ] FFFE COPPERWAIT  DROP   0400 18E COPPERMOVE  DROP
  243.   0 [ DECIMAL ] 105 [ HEX ] FFFE COPPERWAIT  DROP   0500 18E COPPERMOVE  DROP
  244.   0 [ DECIMAL ] 120 [ HEX ] FFFE COPPERWAIT  DROP   0600 18E COPPERMOVE  DROP
  245.   0 [ DECIMAL ] 135 [ HEX ] FFFE COPPERWAIT  DROP   0700 18E COPPERMOVE  DROP
  246.   0 [ DECIMAL ] 150 [ HEX ] FFFE COPPERWAIT  DROP   0800 18E COPPERMOVE  DROP
  247.   0 [ DECIMAL ] 165 [ HEX ] FFFE COPPERWAIT  DROP   0900 18E COPPERMOVE  DROP
  248.   0 [ DECIMAL ] 180 [ HEX ] FFFE COPPERWAIT  DROP   0A00 18E COPPERMOVE  DROP
  249.   0 [ DECIMAL ] 195 [ HEX ] FFFE COPPERWAIT  DROP   0B00 18E COPPERMOVE  DROP
  250.   0 [ DECIMAL ] 210 [ HEX ] FFFE COPPERWAIT  DROP   0C00 18E COPPERMOVE  DROP
  251.   0 [ DECIMAL ] 225 [ HEX ] FFFE COPPERWAIT  DROP   0D00 18E COPPERMOVE  DROP
  252.   0 [ DECIMAL ] 240 [ HEX ] FFFE COPPERWAIT  DROP   0E00 18E COPPERMOVE  DROP
  253.   0 [ DECIMAL ] 255 [ HEX ] FFFE COPPERWAIT  DROP   0F00 18E COPPERMOVE  DROP
  254.   [ DECIMAL ]
  255.   COPPEREND
  256.   CopperLength D!   Copper D!
  257.   ADDCOPPER
  258.   ;
  259.  
  260.   : Free_Copper                  \ Closes down and frees copperlist memory
  261.  
  262.   RemCopper
  263.   Copper D@ FREEMEMORY
  264.   ;
  265.  
  266.   \ ***********************************
  267.   \ Create Display and Slice structures
  268.   \ ***********************************
  269.  
  270.   \ This routine simply makes blank structures, which then need to be
  271.   \ initialised later (in the CREATE_DISPLAY routine).
  272.  
  273.   : CREATE_DSLICES
  274.  
  275.   DS_SIZEOF MAKESTRUCTURE Display1 MAKEPOINTER  \ Main "Display" structure
  276.  
  277.   SL_SIZEOF MAKESTRUCTURE Slice1   MAKEPOINTER  \ Display "Slice" structure
  278.   ;
  279.  
  280.   : FREE_DSLICES
  281.  
  282.   Slice1    DDUP FREEMEMORY   CLEARPOINTER
  283.   Display1  DDUP FREEMEMORY   CLEARPOINTER
  284.   ;
  285.  
  286.   \ ******************************
  287.   \ Create RasInfo structures etc.
  288.   \ ******************************
  289.  
  290.   : CREATE_RASINFO
  291.  
  292.   \ First allocate and initialise complete RasInfo structures.
  293.   \
  294.   \ This routine automatically allocates all BitMaps etc.
  295.   \
  296.  
  297.   Slice1RasterWidth Slice1RasterHeight Slice1Planes  OPENRASINFO
  298.   DFLAG0= ERROR" Fail: RasInfo1"
  299.   Slice1_RasInfo MAKEPOINTER
  300.  
  301.   \ Set invisible area "sprite margins" for slice RasInfo
  302.  
  303.   16              Slice1_RasInfo         ri_RxOffset    INDEX!L
  304.   16              Slice1_RasInfo         ri_RyOffset    INDEX!L
  305.  
  306.   \ Store BitMap pointer - often useful for later reference
  307.  
  308.   Slice1_RasInfo  ri_BitMap INDEXD@L Slice1_BMap MAKEPOINTER
  309.   ;
  310.  
  311.   : FREE_RASINFO
  312.  
  313.   Slice1_RasInfo   DDUP CLOSERASINFO   CLEARPOINTER
  314.   ;
  315.  
  316.   \ ********************************
  317.   \ Create Display/Slice Copperlists
  318.   \ ********************************
  319.  
  320.   \ This function builds the main display copperlist by:
  321.   \
  322.   \ 1. Initialising the Slice data structure
  323.   \ 2. Calling MAKECOPSTRIP for the slice, to build a copperlist
  324.   \ 3. Calling MAKEDISPLAY to build the master Display copperlist
  325.   \
  326.  
  327.   : CREATE_DISPLAY
  328.  
  329.   \ First initialise main display structures
  330.  
  331.   Slice1                           Display1  DS_Slice     INDEXD!L
  332.  
  333.   Slice1Width                      Slice1    SL_DWidth    INDEX!L
  334.   Slice1Height                     Slice1    SL_DHeight   INDEX!L
  335.   DisplayTopLine                   Slice1    SL_DyOffset  INDEX!L
  336.   Slice1_RasInfo                   Slice1    SL_RasInfo   INDEXD!L
  337.   Slice1_ColorMap                  Slice1    SL_ColorMap  INDEXD!L
  338.   Slice1Mode                       Slice1    SL_Modes     INDEX!L
  339.  
  340.   \ Generate copper list information for each of the display slices
  341.  
  342.   Slice1 MAKECOPSTRIP
  343.   D0= ERROR" Fail: Slice1CopStrip"
  344.  
  345.   \ Make display
  346.  
  347.   Display1 MAKEDISPLAY
  348.   D0= ERROR" Fail: Display1"
  349.   ;
  350.  
  351.   : FREE_DISPLAY
  352.  
  353.   Display1                 FREEDISPLAY
  354.   Slice1                   FREECOPSTRIP
  355.   ;
  356.  
  357.   \ ********************************************************
  358.   \ Create SliceControl structures for double buffered slice
  359.   \ ********************************************************
  360.  
  361.   \ SliceControl structures are used to control any slices which perform
  362.   \ mapping or scrolling functions, or which require double or triple
  363.   \ playfield buffering.
  364.   \
  365.   \ In this case we have one slice which does not scroll, is not mapped,
  366.   \ but IS double buffered.
  367.   \
  368.  
  369.   : CREATE_SLICECONTROL
  370.  
  371.   \ Make SliceControl for double buffered bitmap display
  372.  
  373.   Slice1  0 0 MAKESLICECONTROL
  374.   DFLAG0= ERROR" Fail: SliceControl1"
  375.   Slice1_SliceControl MAKEPOINTER
  376.  
  377.   \ Install slice controls into HeliOS display control system
  378.  
  379.   Slice1_SliceControl  INSTALLSLICECONTROL
  380.   ;
  381.  
  382.   : FREE_SLICECONTROL
  383.  
  384.   CLEARSLICECONTROLS
  385.   Slice1_SliceControl  CLOSESLICECONTROL
  386.   ;
  387.  
  388.   \ These routines load an IFF picture into supplied BitMap, and correctly
  389.   \ initialises the supplied ColorTable.
  390.   \
  391.   \ The ColorTable is then used to create an initialised ColorMap structure.
  392.   \
  393.  
  394.   : CREATE_IMAGERY
  395.  
  396.   Slice1_BMap
  397.   Slice1_ColorTable
  398.   Slice1Pic
  399.   10 2 DOSLIB                        \ Call to internal HeliOS library
  400.   10 <> ERROR" Fail: Slice1Pic"
  401.  
  402.   Slice1_ColorTable  Slice1Colours MAKECOLORMAP  \ Allocate ColourMap
  403.   DFLAG0= ERROR" Fail: Slice1ColorMap"
  404.   Slice1_ColorMap MAKEPOINTER
  405.   ;
  406.  
  407.   : FREE_IMAGERY
  408.  
  409.   Slice1_ColorMap  DDUP FREECOLORMAP  CLEARPOINTER
  410.   ;
  411.  
  412.   \ **************
  413.   \ Animation Demo
  414.   \ **************
  415.  
  416.   \ ************************
  417.   \ Animation Demo constants
  418.   \ ************************
  419.  
  420.   \ Set up a number of constants which determine various aspects of the
  421.   \ demo, e.g. How many Bullets required
  422.   \
  423.   \ Change these values as required
  424.   \
  425.  
  426.   15 CONSTANT Bullet#           \ Number of Bullets available
  427.   15 CONSTANT Alien#            \ Number of Aliens available
  428.   8  CONSTANT BulletSpeed       \ Speed of Bullet
  429.   6  CONSTANT FiringRate        \ Speed of gun reload
  430.   4  CONSTANT GunSpeed          \ Speed of Gun movement
  431.   8  CONSTANT AlienMaxXSpeed    \ Maximum speed of Alien movement
  432.   4  CONSTANT AlienMaxYSpeed    \ Maximum speed of Alien movement
  433.  
  434.   \ ****************************************************
  435.   \ Create animation demo pointers and storage variables
  436.   \ ****************************************************
  437.  
  438.   0. DPOINTER GunSpriteSet         \ Gun sprite image
  439.   0. DPOINTER BulletSpriteSet      \ Bullet sprite image
  440.  
  441.   CREATE AlienSpriteSetTable       \ Alien sprite set storage table
  442.   Alien# 4* 0 ALLOTFILL            \ Space allocated = Number_of_Aliens*4
  443.                                    \ This table has to hold a number of
  444.                                    \ 32-bit (4-byte) numbers, because each
  445.                                    \ Alien sprite set is a 32-bit pointer
  446.  
  447.   CREATE AlienAnimTable            \ Alien animation storage table
  448.   Alien# 4* 0 ALLOTFILL            \ Space allocated = Number_of_Aliens*4
  449.                                    \ This table has to hold a number of
  450.                                    \ 32-bit (4-byte) numbers, because
  451.                                    \ each Alien anim is a 32-bit pointer
  452.  
  453.   CREATE BulletTable               \ Bullet sprite pointer storage table.
  454.   Bullet# 4* 0 ALLOTFILL           \ Space allocated = Number_of_Bullets*4
  455.                                    \ This table has to hold a number of
  456.                                    \ 32-bit (4-byte) numbers, because
  457.                                    \ each Bullet sprite is a 32-bit pointer
  458.  
  459.   CREATE  AlienCollHandlerTable    \ Alien collision handler table
  460.   Alien# 4* 0 ALLOTFILL
  461.  
  462.   CREATE  BulletCollHandlerTable   \ Bullet collision handler table
  463.   Bullet# 4* 0 ALLOTFILL
  464.  
  465.   0 VARIABLE     FireTimer         \ Gun reload rate store
  466.  
  467.  \ **************************************************
  468.  \ This routine is called to delay the Alien's return
  469.  \ **************************************************
  470.  
  471.   : AlienDelay
  472.  
  473.   SETSTRUCTURE3                                \ STRUCTURE3 = Alien pointer
  474.   50  SpriteAnim_CountDown   STRUCTURE3 !L     \ Wait 50 VBlanks
  475.   0   SpriteAnim_CDForth     STRUCTURE3 !L     \ Remove CD action word
  476.   -3  SpriteAnim_CDFlags     STRUCTURE3 !L     \ -3 = Remove
  477.   ;
  478.  
  479.   \ *************************
  480.   \ Sprite collision routines
  481.   \ *************************
  482.  
  483.   \ This routine is called when a Bullet collides.
  484.  
  485.   : BulletHit
  486.  
  487.   2 DDROPS                        \ Drop collisionmask and hit-object pointer
  488.  
  489.   DDUP SpriteCtrl_CollFlag D+ 0!L \ Disable further collisions
  490.   REMOVESPRITE                    \ Remove Bullet
  491.   ;
  492.  
  493.   \ This routine is called when the Alien collides.
  494.  
  495.   : AlienHit
  496.  
  497.   2 DDROPS     \ Drop collisionmask and "hitting" object
  498.  
  499.   SpriteCtrl_Anim D+ D@L SETSTRUCTURE4
  500.  
  501.   0   SpriteAnim_CollFlag    STRUCTURE4 !L  \ Disable further collisions
  502.   0   SpriteAnim_UserData1   STRUCTURE4 !L  \ Stop X-movement (inc = 0)
  503.   0   SpriteAnim_UserData2   STRUCTURE4 !L  \ Stop Y-movement (inc = 0)
  504.   2   SpriteAnim_Speed       STRUCTURE4 !L  \ Set explosion anim speed
  505.   9   SpriteAnim_Frames      STRUCTURE4 !L  \ Number of frames
  506.   0   SpriteAnim_Current     STRUCTURE4 !L  \ Start at frame 0
  507.   20  SpriteAnim_CountDown   STRUCTURE4 !L  \ Run for 20 VBlanks
  508.   -2  SpriteAnim_CDFlags     STRUCTURE4 !L  \ Flags set after CountDown
  509.                                             \ -2 = Go invisible
  510.  
  511.   ' AlienDelay  CFA                         \ Get pointer to delay code
  512.       SpriteAnim_CDForth     STRUCTURE4 !L  \ Countdown code
  513.   ;
  514.  
  515.   \ ***************************
  516.   \ Create HitMasks and MeMasks
  517.   \ ***************************
  518.  
  519.   \ Collisions can only occur between objects which have coincident bits
  520.   \ set in HitMask-MeMask or MeMask-HitMask pairs
  521.   \
  522.   \ Each bit of the HitMask/MeMask pair have a corresponding position in
  523.   \ a collision table which stores an array of collision routines.
  524.   \
  525.   \ In the event of a collision, the routine in an object's Collision Table
  526.   \ corresponding to the HitMask-MeMask bit coincidence will be called
  527.   \ with 3 parameters: the two colliding objects and the collision HitMask
  528.   \
  529.   \ The stack on a collision call looks like this:
  530.   \
  531.   \ CollisionHitMask(l) = Top 32-bit value on stack
  532.   \ CollidingObject(l)  = 2nd 32-bit value on stack
  533.   \ This Object(l)      = 3rd 32-bit value on stack
  534.   \
  535.   \ Here are two examples:
  536.   \
  537.   \   00000000000000000000000000000010.  = Object A HitMask
  538.   \   00000000000000000000000000000000.  = Object B HitMask
  539.   \   00000000000000000000000000000001.  = Object C HitMask
  540.   \
  541.   \   00000000000000000000000000000000.  = Object A MeMask
  542.   \   00000000000000000000000000000011.  = Object B MeMask
  543.   \   00000000000000000000000000000000.  = Object C MeMask
  544.   \
  545.   \ Would allow A and B to collide, B and C to collide, but not A and C.
  546.   \
  547.   \ B and C would run the routine at position 1 in the collision table
  548.   \ A and B would run the routine at position 2 in the collision table
  549.   \
  550.   \ -----------------------------------------------------------
  551.   \
  552.   \   00000000000000000000000000000011.  = Object A HitMask
  553.   \   00000000000000000000000000000001.  = Object B HitMask
  554.   \   00000000000000000000000000000001.  = Object C HitMask
  555.   \
  556.   \   00000000000000000000000000000000.  = Object A MeMask
  557.   \   00000000000000000000000000000000.  = Object B MeMask
  558.   \   00000000000000000000000000000010.  = Object C MeMask
  559.   \
  560.   \ Would allow just A and C to collide
  561.   \
  562.   \ A and C would run the routine at position 2 in the collision table
  563.   \
  564.  
  565.   BIN
  566.  
  567.   00000000000000000000000000000001.  DCONSTANT  BulletHitMask
  568.   00000000000000000000000000000000.  DCONSTANT  AlienHitMask
  569.  
  570.   00000000000000000000000000000000.  DCONSTANT  BulletMeMask
  571.   00000000000000000000000000000001.  DCONSTANT  AlienMeMask
  572.  
  573.   DECIMAL
  574.  
  575.  
  576.   CREATEL BulletCollTable
  577.   -1 ,                        \ -1 = The "1" means there is 1 entry in table
  578.                               \      The "-" signifies that it is a HeliOS
  579.                               \      word rather than machine code
  580.  
  581.   FIND BulletHit ,            \ Find BulletHit and store HeliOS word CFA
  582.  
  583.   CREATEL AlienCollTable
  584.   -1 ,                        \ -1 = The "1" means there is 1 entry in table
  585.                               \      The "-" signifies that it is a HeliOS
  586.                               \      word rather than machine code
  587.  
  588.   FIND AlienHit ,             \ Find AlienHit and store HeliOS word CFA
  589.  
  590.  
  591.   \ *************************
  592.   \ Set up collision handlers
  593.   \ *************************
  594.  
  595.   : CREATE_COLLISIONS
  596.  
  597.   Bullet# 0
  598.   DO
  599.     GETCOLLHANDLER
  600.     DFLAG0= ERROR" Fail: Bullet CollHandler"
  601.     DDUP BulletCollHandlerTable I 4* + D!
  602.     BulletTable I 4* + D@  SpriteCtrl_CollHandler  INDEXD!L
  603.   LOOP
  604.  
  605.   Alien# 0
  606.   DO
  607.     GETCOLLHANDLER
  608.     DFLAG0= ERROR" Fail: Alien CollHandler"
  609.     DDUP AlienCollHandlerTable I 4* + D!
  610.     AlienAnimTable I 4* + D@  SpriteAnim_CollHandler  INDEXD!L
  611.   LOOP
  612.   ;
  613.  
  614.   : FREE_COLLISIONS
  615.  
  616.   Alien# 0
  617.   DO
  618.     Alien# 1- I - 4*
  619.     AlienCollHandlerTable + D@
  620.     FREECOLLHANDLER
  621.   LOOP
  622.  
  623.   Bullet# 0
  624.   DO
  625.     Bullet# 1- I - 4*
  626.     BulletCollHandlerTable + D@
  627.     FREECOLLHANDLER
  628.   LOOP
  629.   ;
  630.  
  631.   \ *****************
  632.   \ Boundary routines
  633.   \ *****************
  634.  
  635.   \ Alien boundary sense routine
  636.  
  637.   : AlienBoundary
  638.  
  639.   DDUP SpriteCtrl_Anim D+ D@L SETSTRUCTURE5
  640.   SpriteCtrl_VisiHit INDEX@L                 \ Get boundary hit mask word
  641.   0 BTST                                     \ Did it hit left boundary
  642.   IF
  643.     SpriteAnim_UserData1 STRUCTURE5 @L       \ Reverse X-motion
  644.     NEGATE
  645.     SpriteAnim_UserData1 STRUCTURE5 !L
  646.   ELSE
  647.     1 BTST                                   \ Did it hit right boundary
  648.     IF
  649.       SpriteAnim_UserData1 STRUCTURE5 @L     \ Reverse X-motion
  650.       NEGATE
  651.       SpriteAnim_UserData1 STRUCTURE5 !L
  652.     THEN
  653.   THEN
  654.  
  655.   2 BTST                                     \ Did it hit upper boundary
  656.   IF
  657.     DROP
  658.     SpriteAnim_UserData2 STRUCTURE5 @L       \ Reverse Y-motion
  659.     NEGATE
  660.     SpriteAnim_UserData2 STRUCTURE5 !L
  661.   ELSE
  662.     3 BTST                                   \ Did it hit lower boundary
  663.     IF
  664.       DROP
  665.       SpriteAnim_UserData2 STRUCTURE5 @L     \ Reverse Y-motion
  666.       NEGATE
  667.       SpriteAnim_UserData2 STRUCTURE5 !L
  668.     ELSE
  669.       DROP
  670.     THEN
  671.   THEN
  672.   ;
  673.  
  674.   \ Remove Bullet - called when bullet hits boundary at edge of display
  675.  
  676.   : BulletRemove
  677.  
  678.   RemoveSprite
  679.   ;
  680.  
  681.   \ ****************************
  682.   \ User input response routines
  683.   \ ****************************
  684.  
  685.   : Fire
  686.  
  687.   RAWKEY @ 64 =
  688.   JOY1FIRE OR
  689.   IF
  690.     FireTimer @ 0<
  691.     IF
  692.       FiringRate FireTimer !
  693.       Bullet# 0
  694.       DO
  695.         BulletTable I 4* + D@ SETSTRUCTURE1
  696.         SpriteCtrl_Running STRUCTURE1 @L 0=
  697.         IF
  698.           1 SpriteCtrl_Running STRUCTURE1 !L
  699.           GunSpriteSet 8. D+ D@L SETSTRUCTURE2
  700.  
  701.           SpriteCtrl_XPos STRUCTURE2 @L 5 +
  702.           SpriteCtrl_XPos STRUCTURE1 !L
  703.  
  704.           SpriteCtrl_YPos STRUCTURE2 @L 2 -
  705.           SpriteCtrl_YPos STRUCTURE1 !L
  706.  
  707.           SpriteCtrl_UserData1 STRUCTURE1
  708.           SpriteCtrl_YAdd STRUCTURE1 D!L
  709.  
  710.           BulletSpeed NEGATE SpriteCtrl_UserData1 STRUCTURE1 !L
  711.  
  712.           1 SpriteCtrl_CollFlag STRUCTURE1 !L
  713.           1 SpriteCtrl_Flags STRUCTURE1 !L
  714.  
  715.           0. STRUCTURE1 INSTALLSPRITE
  716.           LEAVE
  717.         THEN
  718.       LOOP
  719.     ELSE
  720.       FireTimer DEC
  721.     THEN
  722.   THEN
  723.   ;
  724.  
  725.   : MoveGun
  726.  
  727.   RAWKEY @ 78 =
  728.   JOY1LEFTRIGHT 0>
  729.   OR
  730.   IF
  731.      GunSpeed
  732.      19 320
  733.      GunSpriteSet 8. D+ D@L SpriteCtrl_XPos D+
  734.      LIMIT+!L
  735.   ELSE
  736.     RAWKEY @ 79 =
  737.     JOY1LEFTRIGHT 0<
  738.     OR
  739.     IF
  740.      GunSpeed NEGATE
  741.      19 320
  742.      GunSpriteSet 8. D+ D@L SpriteCtrl_XPos D+
  743.      LIMIT+!L
  744.     THEN
  745.   THEN
  746.   ;
  747.  
  748.   \ ***********************************
  749.   \ Create sprite and animation objects
  750.   \ ***********************************
  751.  
  752.   : CREATE_SPRITES
  753.  
  754.   \ ----------
  755.   \ Gun sprite
  756.   \ ----------
  757.  
  758.   Slice1_BMap
  759.   154 272
  760.   13 15
  761.   1
  762.   MAKESPRITESET
  763.   DFLAG0= ERROR" Fail: Gun SpriteSet"
  764.   GunSpriteSet MAKEPOINTER
  765.  
  766.   GunSpriteSet 8. D+ D@L
  767.   DDUP  SpriteCtrl_XPos D+       170 -ROT !L
  768.         SpriteCtrl_YPos D+       239 -ROT !L
  769.  
  770.   Slice1_SliceControl   GunSpriteSet    INITSPRITESET
  771.  
  772.   \ -------------
  773.   \ Bullet sprite
  774.   \ -------------
  775.  
  776.   Slice1_BMap
  777.   159 279
  778.   3 7
  779.   1
  780.   MAKESPRITESET
  781.   DFLAG0= ERROR" Fail: Bullet SpriteSet"
  782.   BulletSpriteSet MAKEPOINTER
  783.  
  784.   Slice1_SliceControl   BulletSpriteSet    INITSPRITESET
  785.  
  786.   Bullet# 0
  787.   DO
  788.     BulletSpriteSet 8. D+ D@L CLONESPRITE
  789.     DFLAG0= ERROR" Fail: Bullet Sprite"
  790.     DDUP BulletTable I 4* + D!
  791.     SETSTRUCTURE1
  792.  
  793.     BulletCollTable        SpriteCtrl_CollTable      STRUCTURE1 D!L
  794.     BulletHitMask          SpriteCtrl_HitMask        STRUCTURE1 D!L
  795.     BulletMeMask           SpriteCtrl_MeMask         STRUCTURE1 D!L
  796.     -2                     SpriteCtrl_VisiZone       STRUCTURE1 !L
  797.     4                      SpriteCtrl_LeftZone       STRUCTURE1 !L
  798.     4                      SpriteCtrl_RightZone      STRUCTURE1 !L
  799.     4                      SpriteCtrl_UpZone         STRUCTURE1 !L
  800.     10                     SpriteCtrl_DownZone       STRUCTURE1 !L
  801.     ' BulletRemove CFA     SpriteCtrl_VisiForth      STRUCTURE1 !L
  802.   LOOP
  803.  
  804.   \ ----------------
  805.   \ Alien animations
  806.   \ ----------------
  807.  
  808.   Alien# 0
  809.   DO
  810.  
  811.   \ -------------
  812.   \ Alien sprites
  813.   \ -------------
  814.  
  815.     Slice1_BMap
  816.     24 276
  817.     14 9
  818.     9
  819.     MAKESPRITESET
  820.     DFLAG0= ERROR" Fail: Alien SpriteSet"
  821.     DDUP AlienSpriteSetTable I 4* + D!
  822.  
  823.     Slice1_SliceControl  DSWAP  INITSPRITESET
  824.  
  825.     SpriteAnim_SIZEOF MAKESTRUCTURE
  826.     DFLAG0= ERROR" Fail: AlienAnim"
  827.     DDUP AlienAnimTable I 4* + D!
  828.     SETSTRUCTURE6
  829.  
  830.     AlienSpriteSetTable I 4* + D@ 8. D+
  831.                           SpriteAnim_Image          STRUCTURE6 D!L
  832.     AlienCollTable        SpriteAnim_CollTable      STRUCTURE6 D!L
  833.     AlienHitMask          SpriteAnim_HitMask        STRUCTURE6 D!L
  834.     AlienMeMask           SpriteAnim_MeMask         STRUCTURE6 D!L
  835.     6                     SpriteAnim_AnimControl    STRUCTURE6 !L
  836.     1                     SpriteAnim_Skip           STRUCTURE6 !L
  837.     1                     SpriteAnim_VisiZone       STRUCTURE6 !L
  838.     4                     SpriteAnim_LeftZone       STRUCTURE6 !L
  839.     4                     SpriteAnim_RightZone      STRUCTURE6 !L
  840.     4                     SpriteAnim_UpZone         STRUCTURE6 !L
  841.     25                    SpriteAnim_DownZone       STRUCTURE6 !L
  842.     ' AlienBoundary CFA   SpriteAnim_VisiForth      STRUCTURE6 !L
  843.     SpriteAnim_UserData1 STRUCTURE6 SpriteAnim_XAdd STRUCTURE6 D!L
  844.     SpriteAnim_UserData2 STRUCTURE6 SpriteAnim_YAdd STRUCTURE6 D!L
  845.   LOOP
  846.   ;
  847.  
  848.   : FREE_SPRITES
  849.  
  850.   Alien# 0
  851.   DO
  852.     Alien# 1- I - 4* AlienAnimTable +
  853.     DUP
  854.     D@ FREEMEMORY
  855.     D0!
  856.  
  857.     Alien# 1- I - 4* AlienSpriteSetTable +
  858.     DUP
  859.     D@ FREESPRITESET
  860.     D0!
  861.   LOOP
  862.  
  863.   Bullet# 0
  864.   DO
  865.     Bullet# 1- I - 4* BulletTable +
  866.     DUP
  867.     D@ FREESPRITE
  868.     D0!
  869.   LOOP
  870.  
  871.   BulletSpriteSet DDUP FREESPRITESET CLEARPOINTER
  872.  
  873.   GunSpriteSet    DDUP FREESPRITESET CLEARPOINTER
  874.   ;
  875.  
  876.   \ *****************
  877.   \ Re-Install Aliens
  878.   \ *****************
  879.  
  880.   : ?Install_Aliens
  881.  
  882.   \ Check to see if any Aliens are still running
  883.  
  884.   1                                             \ Put 1 flag on stack
  885.   Alien# 0                                      \ Loop for all Aliens
  886.   DO
  887.     AlienAnimTable I 4* + D@ SETSTRUCTURE7
  888.     SpriteAnim_Running STRUCTURE7 @L            \ If an Alien is running
  889.     IF
  890.       DROP 0                                    \ Change '1' on stack to '0'
  891.     THEN
  892.   LOOP
  893.  
  894.   \ If any Alien was running flag will now be '0' so next code is skipped
  895.  
  896.   IF
  897.     Alien# 0
  898.     DO
  899.       AlienAnimTable I 4* + D@ SETSTRUCTURE7
  900.       0                           SpriteAnim_CDForth     STRUCTURE7 !L
  901.       0                           SpriteAnim_CountDown   STRUCTURE7 !L
  902.       1                           SpriteAnim_Running     STRUCTURE7 !L
  903.       1                           SpriteAnim_Flags       STRUCTURE7 !L
  904.       1                           SpriteAnim_CollFlag    STRUCTURE7 !L
  905.       280 RND 26 +                SpriteAnim_XPos        STRUCTURE7 !L
  906.       36                          SpriteAnim_YPos        STRUCTURE7 !L
  907.       0                           SpriteAnim_Current     STRUCTURE7 !L
  908.       2                           SpriteAnim_Speed       STRUCTURE7 !L
  909.       4                           SpriteAnim_Frames      STRUCTURE7 !L
  910.       AlienMaxXSpeed RND 2 MAX 1 RND IF NEGATE THEN
  911.                                   SpriteAnim_UserData1   STRUCTURE7 !L
  912.       AlienMaxYSpeed RND 1 MAX 1 RND IF NEGATE THEN
  913.                                   SpriteAnim_UserData2   STRUCTURE7 !L
  914.       0. STRUCTURE7 INSTALLSPRITEANIM
  915.     LOOP
  916.   THEN
  917.   ;
  918.  
  919.   \ *********************
  920.   \ Close down everything
  921.   \ *********************
  922.  
  923.   : CLOSEDOWN
  924.  
  925.   FREE_COLLISIONS
  926.   FREE_SPRITES
  927.   FREE_COPPER
  928.   FREE_SLICECONTROL
  929.   FREE_DISPLAY
  930.   FREE_IMAGERY
  931.   FREE_RASINFO
  932.   FREE_DSLICES
  933.   RESETERROR"
  934.   ;
  935.  
  936.   LATESTCFA (CLOSEDOWN) !
  937.  
  938.   : TestDisplay          \ Start of program
  939.  
  940.   SCRCLR
  941.   CR
  942.  
  943.   ."        **********************************************************"
  944.   CR 6 FPENSET
  945.   ."                           MULTI ANIMATION DEMO"
  946.   CR 1 FPENSET
  947.   ."        **********************************************************"
  948.   CR
  949.   CR
  950.   ."        This code demonstrates how to create multiple animations and"
  951.   CR
  952.   ."        then do collision detection."
  953.   CR
  954.   CR
  955.   ."        This code builds upon the following Demos:"
  956.   CR
  957.   CR
  958.   ."        Demo1_SinglePF.src"
  959.   CR
  960.   ."        Demo2_SinglePFCopper.src"
  961.   CR
  962.   ."        Demo3_SimpleSprite.src"
  963.   CR
  964.   ."        Demo4_MultiSprites.src"
  965.   CR
  966.   ."        Demo5_MultiSptCollide.src"
  967.   CR
  968.   ."        Demo6_SimpleAnim.src"
  969.   CR
  970.   CR
  971.   ."        **********************************************************"
  972.   CR 6 FPENSET
  973.   ."                  Press <Space> or <L-Mouse> to see Demo"
  974.   CR 3 FPENSET
  975.   ."          Use a Joystick to move the gun turret and fire bullets"
  976.   CR
  977.   ."        **********************************************************"
  978.   CR
  979.  
  980.   WAITSPACE
  981.  
  982.   SCRCLR
  983.  
  984.   ERROR1 SETERROR"       \ Redirect system errors to our routine ERROR1
  985.  
  986.   CREATE_DSLICES
  987.   CREATE_RASINFO
  988.   CREATE_IMAGERY
  989.   CREATE_DISPLAY
  990.   CREATE_SLICECONTROL
  991.   CREATE_COPPER
  992.   CREATE_SPRITES
  993.   CREATE_COLLISIONS
  994.  
  995.   HeliOS_On
  996.  
  997.   GunSpriteSet 8. D+ D@L INSTALLSPRITE
  998.  
  999.   1 FrameRate !L
  1000.  
  1001.   Display1 SHOWDISPLAY
  1002.  
  1003.   BEGIN
  1004.     WAITFRAME
  1005.  
  1006.     ?Install_Aliens
  1007.  
  1008.     Fire
  1009.  
  1010.     MoveGun
  1011.  
  1012.     ?TERMINAL 27 =
  1013.   UNTIL
  1014.  
  1015.   -3 GunSpriteSet 8. D+ D@L SpriteCtrl_Flags INDEX!L
  1016.  
  1017.   Alien# 0
  1018.   DO
  1019.   -3 AlienAnimTable I 4* + D@ SpriteAnim_Flags INDEX!L
  1020.   LOOP
  1021.  
  1022.   Bullet# 0
  1023.   DO
  1024.     -3 BulletTable I 4* + D@ SpriteCtrl_Flags INDEX!L
  1025.   LOOP
  1026.  
  1027.   5 DELAY
  1028.  
  1029.   HeliOS_Off
  1030.  
  1031.   CLOSEDOWN
  1032.   ;
  1033.  
  1034.   TestDisplay
  1035.